// pages/material/pages/basis/form_b/index.js
var app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        id:0,
        number_goods: 0,
        identity: '', //办理身份
        identity_show: false, //显示隐藏
        identity_txt: '',
        //申请人资料
        full_name: '', //姓名
        phone_number: '', //电话
        idCard_z: '', //身份证（正）
        idCard_f: '', //身份证（反）
        household: '', //户口本

        household_files: [], //户口本
        household_max: 9,
        household_max_vir: 9, //最多上传张数 


        s_idCard_z: '', //代理人身份证（正）
        image_src: {
            
        },
        s_idCard_f: '', //代理人身份证（反）

        //下面是父母的资料
        marriage: '', //婚姻状态
        marriage_license: '', //结婚证
        marriage_files: [], //结婚证多图
        marriage_max: 9, //最多上传张数
        marriage_max_vir: 9, //最多上传张数

        divorce: '', //离婚证
        divorce_files: [], //离婚协议
        divorce_max: 9, //最多上传张数
        divorce_max_vir: 9, //最多上传张数
        
        idCard_z_f: '', //父亲身份证（正）
        idCard_f_f: '', //父亲身份证（反）
        idCard_z_m: '', //母亲身份证（正）
        idCard_f_m: '', //母亲身份证（反）
        household_parents: '', //户口本

        parents_household_files: [], //户口本
        parents_household_max: 9,
        parents_household_max_vir: 9, //最多上传张数 

        other_files: [], //其他
        other_max: 9, //最多上传张数
        other_max_vir: 9, //最多上传张数
    },
    //办理身份选择
    identityChange(e) {
        let value = e.detail.value
        this.setData({
            identity_show: true,
            identity: value
        })
        if (value == 1) {
            this.setData({
                identity_txt: '申请人',
            })
        } else if (value == 2) {
            this.setData({
                identity_txt: '代理人',
            })
        }

    },
    set_input: function (e) {},
    //图片选择
    choose_images: function (e) {
        var that = this;
        let source = e.currentTarget.dataset.source; //当前占位图名字
        let type = e.currentTarget.dataset.type; // 类型（单图/多图）
        console.log(e.currentTarget.dataset);
        if (type == 0) { //单图
            wx.chooseImage({
                count: 1,
                sizeType: ['original', 'compressed'],
                sourceType: ['album', 'camera'],
                success: res => {
                    var that_ = that
                    const src = res.tempFilePaths
                    var upload_img = source
                    
                    that.setData({
                        [upload_img]: src
                    });
                    var tempFilePaths = res.tempFilePaths
                    for (var i = 0; i < tempFilePaths.length; i++) {
                        wx.uploadFile({
                          //  url: 'https://gz.gzrzvisa.com/data/api.upload/add',
                            url: 'https://gz.gzrzvisa.com/data/api.upload/add',
                            filePath: tempFilePaths[i],
                            name: "file",
                            header: {
                                "content-type": "multipart/form-data"
                            },
                            success: function (res) {
                                
                                if (res.statusCode == 200) {
                                    wx.showToast({
                                        title: "上传成功",
                                        icon: "none",
                                        duration: 1500
                                    })
                        
                                    that.data.image_src[upload_img] = res.data
                                    console.log(that.data.image_src[upload_img]);
                                }
                            },
                            fail: function (err) {
                                wx.showToast({
                                title: "上传失败",
                                icon: "none",
                                duration: 2000
                                })
                            },
                            complete: function (result) {
                                console.log(result.errMsg)
                            }
                        })
                    }
                    
                }
            });
        } else if (type == 1) { //多图
            var in_source = source + '_files'; //当前占位图名字
            let in_count = source + '_max_vir'; //当前占位图名字
            let num_vir = e.currentTarget.dataset.num_vir; //限制数值
            wx.chooseImage({
                count: num_vir,
                sizeType: ['original', 'compressed'],
                sourceType: ['album', 'camera'],
                success: res => {
                    const src = res.tempFilePaths;
                    // console.log(src)
                    that.setData({
                        [in_count]: num_vir + 1,
                        [in_source]: that.data[in_source].concat(src)
                    });
                    var tempFilePaths = res.tempFilePaths
                    for (var i = 0; i < tempFilePaths.length; i++) {
                        wx.uploadFile({
                            url: 'https://gz.gzrzvisa.com/data/api.upload/add',
                            filePath: tempFilePaths[i],
                            name: "file",
                            header: {
                                "content-type": "multipart/form-data"
                            },
                            success: function (res) {
                                if (res.statusCode == 200) {
                                    wx.showToast({
                                        title: "上传成功",
                                        icon: "none",
                                        duration: 1500
                                    })
                        
                                    if(typeof(that.data.image_src[in_source]) == 'undefined'){
                                        that.data.image_src[in_source] = [];
                                    }
                                    that.data.image_src[in_source] = that.data.image_src[in_source].concat(res.data)
                                    console.log(that.data.image_src[in_source]);
                                }
                            },
                            fail: function (err) {
                                wx.showToast({
                                title: "上传失败",
                                icon: "none",
                                duration: 2000
                                })
                            },
                            complete: function (result) {
                                console.log(result.errMsg)
                            }
                        })
                    }
                }
            });
        }
    },
    //删除图片
    to_delete: function (e) {
        var that = this;
        var select_type = e.currentTarget.dataset.source; //当前占位图名字

        console.log(select_type);
        that.setData({
            [select_type]: '',
        });
        that.data.image_src[select_type] = '';
    },
    //查看图片
    previewImage: function (e) {
        var source = e.currentTarget.dataset.source;
        wx.previewImage({
            current: e.currentTarget.source, // 当前显示图片的http链接
            urls: source // 需要预览的图片http链接列表
        })
    },
    //多图查看图片
    preview_image: function (e) {
        var that = this
        let files = e.currentTarget.dataset.source
        var img = e.currentTarget.dataset.img
        wx.previewImage({
            current: img, // 当前显示图片的http链接
            urls: that.data[files] // 需要预览的图片http链接列表
        })
    },
    //删除图片
    // to_delete: function (e) {
    //     var that = this;
    //     var index = e.currentTarget.dataset.index;
    //     var images = that.data.files;
    //     images.splice(index, 1);
    //     that.setData({
    //         photo_count: that.data.photo_count + 1,
    //         files: images,
    //     });
        
    // },
    //多图删除
    delete_set: function (e) {
        console.log(e)
        var that = this;
        let source = e.currentTarget.dataset.source;
        var in_source = source + '_files'; //当前占位图名字
        let in_count = source + '_max_vir'; //当前占位图名字
        let num_vir = e.currentTarget.dataset.num_vir; //限制数值
        let index = e.currentTarget.dataset.index;
        let images = that.data[in_source];
        images.splice(index, 1);
        that.setData({
            [in_count]: that.data.photo_count + 1,
            [in_source]: images,
        });
        that.data.image_src[in_source].splice(index, 1)
        console.log(that.data.other_files)
        console.log(that.data.image_src[in_source]);
    },
    //婚姻状况选择
    marriageChange(e) {
        let value = e.detail.value
        console.log(value);
        this.setData({
            marriage: value,
            marriage_files: [], //结婚证
            divorce_files: [], //离婚证
            divorce_agree: [], //离婚协议
        })
    },
    //提交
    to_submit: function (e) {
        var that = this;

        // idCard_z_f: '', //父亲身份证（正）
        // idCard_f_f: '', //父亲身份证（反）
        // idCard_z_m: '', //母亲身份证（正）
        // idCard_f_m: '', //母亲身份证（反）
        // parents_household_files: '', //户口本

        let number_goods = that.data.number_goods;
        let client_name = that.data.full_name; //姓名
        let client_phone = that.data.phone_number; //电话
        let client_front_of_id_card = that.data.image_src.idCard_z; //身份证（正）
        let client_reverse_side_of_id_card = that.data.image_src.idCard_f; //身份证（反）
        let front_of_agents_id_card = that.data.image_src.s_idCard_z; //代理人身份证（正）
        let reverse_side_of_agents_id_card = that.data.image_src.s_idCard_f; //代理人身份证（反）
        let client_household_register = that.data.image_src.household_files; //户口本
        let client_marital_status = that.data.marriage; //婚姻状态
        if(client_marital_status>0){
            client_marital_status = client_marital_status -1;
        }
        // client_marital_status = client_marital_status == '2' ? '1':'2';
        let client_marriage_certificate = that.data.image_src.marriage_files; //结婚证
        // let client_divorce_certificate = that.data.image_src.divorce; //离婚证
        let client_divorce_agreement = that.data.image_src.divorce_files; //离婚协议

        let front_of_fathers_id_card = that.data.image_src.idCard_z_f;//父亲身份证（正）
        let reverse_side_of_fathers_id_card = that.data.image_src.idCard_f_f;//父亲身份证（反）
        let front_of_mothers_id_card = that.data.image_src.idCard_z_m;//母亲身份证（正）
        let reverse_side_of_mothers_id_card = that.data.image_src.idCard_f_m;//母亲身份证（反）

        let parents_household_register = that.data.image_src.parents_household_files; //户口本
        let other = that.data.image_src.other_files; //其他
        let base_type = 1; //委托类型
        let client_number = 1; //委托人数
        let matter_id = that.data.identity; //公证类型
        let notarial_office_id = wx.getStorageSync('notarial_office_id');; //公证处
        let identity_se = that.data.identity == 1 ? '申请人' : '代理人';
        let id = that.data.id
        
        if (matter_id == '') {
            wx.showToast({
                title: '请选择办理身份',
                icon: 'none',
                mask: false,
            });
            return false;
        } else if (client_name == '') {
            wx.showToast({
                title: '请填写' + identity_se + '姓名',
                icon: 'none',
                mask: false,
            });
            return false;
        } else if (client_phone === '') {
            wx.showToast({
                title: '请填写' + identity_se + '联系电话',
                icon: 'none',
                mask: false,
            });
            return false;
        } else if (!(/^(?:13\d|14\d|15\d|16\d|17\d|18\d|19\d)\d{5}(\d{3}|\*{3})$/.test(client_phone))) {
            wx.showToast({
                title: '请输入正确的手机号码',
                icon: 'none',
                mask: true,
                duration: 1500
            });
            return
        }
        // } else if (client_front_of_id_card === '') {
        //     wx.showToast({
        //         title: '请上传身份证正面照',
        //         icon: 'none',
        //         mask: false,
        //     });
        //     return false;
        // } else if (client_reverse_side_of_id_card === '') {
        //     wx.showToast({
        //         title: '请上传身份证背面照',
        //         icon: 'none',
        //         mask: false,
        //     });
        //     return false;
        // } else if (front_of_agents_id_card === '' && that.data.identity == 2) {
        //     wx.showToast({
        //         title: '请上传代理人身份证正面照',
        //         icon: 'none',
        //         mask: false,
        //     });
        //     return false;
        // } else if (reverse_side_of_agents_id_card === '' && that.data.identity == 2) {
        //     wx.showToast({
        //         title: '请上传代理人身份证背面照',
        //         icon: 'none',
        //         mask: false,
        //     });
        //     return false;
        // } else if (client_household_register === '') {
        //     wx.showToast({
        //         title: '请上传户口本图片',
        //         icon: 'none',
        //         mask: false,
        //     });
        //     return false;
        // }
        this.setData({
            disabled: true
        })
        wx.showToast({
            title: '上传中...',
            icon: 'loading',
            mask: true,
            duration: 1500,
            success(res) {
                
                let params = {}
                params.client_name = client_name;
                params.client_phone =client_phone;
                params.client_front_of_id_card =client_front_of_id_card;
                params.client_reverse_side_of_id_card =client_reverse_side_of_id_card;
                params.front_of_agents_id_card =front_of_agents_id_card;
                params.reverse_side_of_agents_id_card =reverse_side_of_agents_id_card;
                params.client_household_register =client_household_register;
                params.client_marital_status =client_marital_status;
                params.client_marriage_certificate =client_marriage_certificate;
                // params.client_divorce_certificate =client_divorce_certificate;
                params.client_divorce_certificate =client_divorce_agreement;

                params.front_of_fathers_id_card =front_of_fathers_id_card;
                params.reverse_side_of_fathers_id_card =reverse_side_of_fathers_id_card;
                params.front_of_mothers_id_card =front_of_mothers_id_card;
                params.reverse_side_of_mothers_id_card =reverse_side_of_mothers_id_card;


                params.parents_household_register =parents_household_register;
                params.other = other;
                params.base_type =base_type;
                params.client_number =client_number;
                params.matter_id =matter_id;
                params.notarial_office_id =notarial_office_id;
                params.number_goods = number_goods;
                // 大于0 修改 0 新增
                params.id = id
                console.log(params)

                

                getApp()._post_form('data/api.auth.order/add', params, (result) => {
                    if (result.code==1) {
                        let order_no = result.data.order_no;
                        setTimeout(() => { //上传成功后
                            wx.redirectTo({
                                url: '/pages/material/pages/pay_status/index?order_no='+order_no,
                            })
                        }, 1500)
                    }else{
                        getApp().showError('传入正确的证件照或网络请求出错');
                    }
                    that.setData({
                        disabled: false
                    })
                },(result) => {
                    console.log(result)
                    
                })
                // setTimeout(() => { //上传成功后
                //     wx.redirectTo({
                //         url: '/pages/material/pages/pay_status/index',
                //     })
                // }, 1500)
            }
        });
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        // console.log(options);
        var id = options.id
        this.getInformation(id);
        // 获取公证类型
        // 购买数量
        if(typeof(options.number_goods) != 'undefined'){
            wx.setStorageSync('number_goods',options.number_goods)
        }
        this.setData({
            number_goods: wx.getStorageSync('number_goods'),
        });
    },
    //获取信息资料
    getInformation:function(id){
        // console.log(id);
        let that = this
        let params = {}
        params.id = id;
        if(id > 0){
            this.setData({
                id: id,
            })
        }else{
            return;
        }
        getApp()._post_form('data/api.auth.order/getInformation', params, (result) => {
            let rt_data = result.data
            // let client_front_of_id_card = that.data.image_src.idCard_z; //身份证（正）
            // let client_reverse_side_of_id_card = that.data.image_src.idCard_f; //身份证（反）
            // let front_of_agents_id_card = that.data.image_src.s_idCard_z; //代理人身份证（正）
            // let reverse_side_of_agents_id_card = that.data.image_src.s_idCard_f; //代理人身份证（反）
            // let client_household_register = that.data.image_src.household_files; //户口本
            // let client_marriage_certificate = that.data.image_src.marriage_files; //结婚证
            // let client_divorce_certificate = that.data.image_src.divorce; //离婚证
            // let client_divorce_agreement = that.data.image_src.divorce_files; //离婚协议

            // let front_of_fathers_id_card = that.data.image_src.idCard_z_f;//父亲身份证（正）
            // let reverse_side_of_fathers_id_card = that.data.image_src.idCard_f_f;//父亲身份证（反）
            // let front_of_mothers_id_card = that.data.image_src.idCard_z_m;//母亲身份证（正）
            // let reverse_side_of_mothers_id_card = that.data.image_src.idCard_f_m;//母亲身份证（反）
            // let parents_household_register = that.data.image_src.parents_household_files; //户口本
            // let other = that.data.image_src.other_files; //其他
            that.data.image_src = {
                idCard_z:rt_data.client_front_of_id_card,
                idCard_f:rt_data.client_reverse_side_of_id_card,
                s_idCard_z:rt_data.front_of_agents_id_card,
                s_idCard_f:rt_data.reverse_side_of_agents_id_card,
                household_files:rt_data.client_household_register,
                marriage_files:rt_data.client_marriage_certificate,
                // divorce:rt_data.client_divorce_certificate,
                divorce_files:rt_data.client_divorce_certificate,
                idCard_z_f:rt_data.front_of_fathers_id_card,
                idCard_f_f:rt_data.reverse_side_of_fathers_id_card,
                idCard_z_m:rt_data.front_of_mothers_id_card,
                idCard_f_m:rt_data.reverse_side_of_mothers_id_card,
                parents_household_files:rt_data.parents_household_register,
                other_files:rt_data.other,

            }
            this.setData({
                identity:rt_data.matter_id,
                identity_txt:rt_data.matter_id == 1 ? '申请人' : '代理人',
                identity_show: true,
                // 委托人信息
                full_name: rt_data.client_name,
                phone_number:rt_data.client_phone,
                idCard_z:rt_data.client_front_of_id_card,
                idCard_f:rt_data.client_reverse_side_of_id_card,
                // 代理人信息
                s_idCard_z:rt_data.front_of_agents_id_card,
                s_idCard_f:rt_data.reverse_side_of_agents_id_card,
                household_files:rt_data.client_household_register,
                // 婚姻状态
                marriage: rt_data.client_marital_status,
                // 结婚证
                marriage_files:rt_data.client_marriage_certificate,
                // 离婚证
                // divorce:rt_data.client_divorce_certificate,
                // 离婚协议
                divorce_files:rt_data.client_divorce_certificate,
                // 父母身份证
                idCard_z_f:rt_data.front_of_fathers_id_card,
                idCard_f_f:rt_data.reverse_side_of_fathers_id_card,
                idCard_z_m:rt_data.front_of_mothers_id_card,
                idCard_f_m:rt_data.reverse_side_of_mothers_id_card,
                // 户口本
                parents_household_files:rt_data.parents_household_register,

                other_files:rt_data.other,
                base_type:1,
                disabled: rt_data.status == 2 ? false:true,
                update_disabled:rt_data.status == 2 ? true:false,
            });
        })
    },
    

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    }
})